package com.airclick.merchant.utils;



import com.alibaba.cloud.commons.lang.StringUtils;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 *
 * @author caiwangjue
 */
public class ToolFun {

	public static String createRandomNum(int length) {
		String randomChars = "1234567890";
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int random = new Random().nextInt(9);
			buffer.append(randomChars.charAt(random));
		}
		return buffer.toString();
	}

	/**
	 * 创建支付平台交易订单号
	 */
	public static String createOrderNum(String prefix) {
		return  prefix + new SimpleDateFormat("yyyyMMddHHmmsss").format(new Date()) + createRandomNum(8);
	}

	/**
	 * 判断传进来的值是否在这几个值之内
	 * true符合
	 * false不符合
	 */
	public static boolean checkValueIn(int param, int... check) {
		boolean flag = false;

		for(int i=0;i<check.length;i++) {
			if(param == check[i]) {
				flag = true;
				break;
			}
		}
		return flag;
	}

	/**
	 *
	 * 对参数判空,若不为空设置值
	 *
	 * @param object
	 * @param fieldName
	 * @param fieldValue
	 */
	public static void vaildParam(Object object, String fieldName, String fieldValue) {
		Method method = null;
		if (!StringUtils.isEmpty(fieldValue)) {
			try {
				method = object.getClass().getMethod(
						"set" + fieldName.substring(0, 1).toUpperCase()
								+ fieldName.substring(1), String.class);
				if (method != null) {
					method.invoke(object, fieldValue);
					System.out.println("设置对象中的属性：" + fieldName + " 值为：" + fieldValue);
				} else {
					System.out.println("查询对象属性失败");
					return;
				}
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("属性名 " + fieldName + " 的值设置错误");
			}
		}
	}

}
